package com.shunli.cterminal.controller;

import com.examples.core.exception.ServiceException;
import com.examples.core.res.CommonResult;
import com.examples.core.res.PagingData;
import com.shunli.cterminal.enums.TfGoodsCodeEnum;
import com.shunli.cterminal.model.code.BusinessResultCode;
import com.shunli.cterminal.model.req.*;
import com.shunli.cterminal.model.res.*;
import com.shunli.cterminal.service.OnlineOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @ClassName: OnlineOrderController
 * @Description: TODO
 * @Author: DAIYALU
 * @Date: 2024/11/9
 * @Version: 1.0
 */
@Tag(name = "管理后台 - 在线订单功能(H5)")
@RequestMapping("/noAuth/order/online")
@RestController
@RequiredArgsConstructor
@Slf4j
public class OnlineOrderController {

    private final OnlineOrderService onlineOrderService;

    @Operation(summary = "获取订单和模板信息")
    @PostMapping("/getAdOrderAndTempConfig")
    public CommonResult<OrderAndTempConfig> getAdOrderAndTempConfig(@RequestBody H5TempInfoReq req,
                                                                    @RequestHeader(value = "userIp",required = false) String userIp) {
        req.setUserIp(userIp);
        try{
            log.info("queryAdTempConfig[], goodsType:{}", req.getGoodsType());
            TfGoodsCodeEnum goodsCodeEnum = TfGoodsCodeEnum.ofCodeNoisy(req.getGoodsType());
        }catch (Exception e){
            throw new ServiceException(BusinessResultCode.GOODS_TYPE_ERROR);
        }
        return CommonResult.data(onlineOrderService.getAdOrderAndTempConfig(req));
    }

    @Operation(summary = "下单(H5)")
    @PostMapping("/create")
    public CommonResult<H5OrderOptVO> create(@RequestHeader(value = "userIp",required = false) String userIp, @RequestBody H5OrderCrtReq req) {
        req.setUserIp(Optional.ofNullable(req.getUserIp()).orElse(userIp));
        return CommonResult.data(onlineOrderService.create(req));
    }

    @Operation(summary = "使用红包")
    @PostMapping("/useRedBag")
    public CommonResult<H5OrderOptVO> useRedBag(@RequestBody H5OrderUseRedBagReq req) {
        return CommonResult.data(onlineOrderService.useRedBag(req));
    }

    @Operation(summary = "订单查询")
    @PostMapping("/myOrder")
    public CommonResult<PagingData<MyOrderInfoVO>> myOrder(@RequestBody MyOrderQueryReq req) {
        return CommonResult.data(onlineOrderService.myOrder(req));
    }

    @Operation(summary = "查看结果页")
    @PostMapping("/viewResPage")
    public CommonResult<String> viewResPage(@RequestBody ViewResPageReq req) {
        return CommonResult.data(onlineOrderService.viewResPage(req));
    }

    @Operation(summary = "查询广告订单")
    @PostMapping("/queryAdOrder")
    public CommonResult<QueryAdOrderVO> queryAdOrder(@RequestParam(value = "orderId") String orderId) {
        return CommonResult.data(onlineOrderService.queryAdOrder(orderId));
    }

    @Operation(summary = "支付订单详情")
    @GetMapping("/getPayedOrderInfo")
    public CommonResult<PayedOrderInfoVO> getPayedOrderInfo(@RequestParam(value = "orderId") String orderId) {
        return CommonResult.data(onlineOrderService.getPayedOrderInfo(orderId));
    }
}
